package com.yingjia.yingjiauniversity.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashSet;
import java.util.Set;

/**
 * @author : dongyiming
 * @version :1.0
 * @description :位置服务SharedPreferences表的操作类
 * @data : 2016/8/23 15:10
 */
public class SpUtils {

    public static final String LOC_TABLE = "common_sp";
    private static SharedPreferences sp;

    private static void init(Context context) {
        if (sp == null) {

            sp = context.getSharedPreferences(LOC_TABLE, Context.MODE_PRIVATE);
        }
    }

    /**
     * 清空sharedpreferences
     */
    public static void clearSharedPre(Context context) {
        if (sp == null) {
            init(context);
        }
        sp.edit().clear().commit();
    }

    /**
     * 判断文件中是否存在这个参数
     */
    public static boolean exitKey(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.contains(key);
    }

    public static void setSharedIntData(Context context, String key, int value) {

        if (sp == null) {

            init(context);
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getSharedIntData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getInt(key, 0);
    }

    public static int getSharedIntData(Context context, String key,
                                       int defaultValue) {
        if (sp == null) {
            init(context);
        }
        return sp.getInt(key, defaultValue);
    }

    public static void setSharedLongData(Context context, String key, long value) {
        if (sp == null)
            init(context);
        sp.edit().putLong(key, value).commit();
    }

    public static long getSharedLongData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getLong(key, 01);
    }

    public static void setSharedFloatData(Context context, String key,
                                          float value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putFloat(key, value).commit();
    }

    public static float getSharedFloatData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getFloat(key, 0f);
    }

    public static float getSharedFloatData(Context context, String key,
                                           float defaultValue) {
        if (sp == null) {
            init(context);
        }
        return sp.getFloat(key, defaultValue);
    }

    public static void setSharedBooleanData(Context context, String key,
                                            boolean value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getSharedBooleanData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getBoolean(key, false);
    }

    public static boolean getSharedBooleanData(Context context, String key,
                                               boolean flag) {
        if (sp == null) {
            init(context);
        }
        return sp.getBoolean(key, flag);
    }

    public static void setSharedStringData(Context context, String key,
                                           String value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getSharedStringData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getString(key, "");
    }

    public static String getSharedStringData(Context context, String key,
                                             String defaultValue) {
        if (sp == null) {
            init(context);
        }
        if (defaultValue == null) {
            defaultValue = "";
        }
        return sp.getString(key, defaultValue);
    }

    public static boolean setSharebooleanData(Context context, String key,
                                              boolean value) {
        if (sp == null) {
            init(context);
        }
        return sp.edit().putBoolean(key, value).commit();

    }

    public static boolean setShareSetData(Context context, String key,
                                          Set<String> values) {
        if (sp == null) {
            init(context);
        }
        return sp.edit().putStringSet(key, values).commit();

    }

    public static Set<String> getSharedSetData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        Set<String> set = new HashSet<String>();
        return sp.getStringSet(key, null);
    }
}
